package com.dyz.leetcode._08_StackAndQueue;

import java.util.ArrayDeque;

public class _71_simplifyPath {
    public String simplifyPath(String path) {
        ArrayDeque<String> stack = new ArrayDeque();
        String[] dirs = path.split("/");
        for(String dir:dirs){
            if(dir.equals("") || dir.equals(".")){
                continue;
            }else if(dir.equals("..") && stack.isEmpty()){
                continue;
            }else if(dir.equals("..")&& !stack.isEmpty()){
                stack.pop();
            }else{
                stack.push(dir);
            }
        }
        if(stack.isEmpty()){return "/";}
        StringBuilder sb = new StringBuilder();

        while(!stack.isEmpty()){
            sb.append("/");
            sb.append(stack.removeLast());
        }
        return sb.toString();
    }
}
